package com.ebelter.temperaturegun.ui.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.Temperature2Manager;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData3;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.WheelView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.WheelViewDialog;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.TgManager;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.model.http.TGNetUtils;
import com.ebelter.temperaturegun.ui.view.ListViewDialog;
import com.ebelter.temperaturegun.ui.view.temview.TemHisChartView;
import com.ebelter.temperaturegun.ui.view.temview.TemHisChartViewForm;
import com.ebelter.temperaturegun.utils.TemGunUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;

public class TemperatureCurveActivity extends TBaseActivity {


    private static final String TAG = "TemperatureCurve";
    private ImageView top_iv;
    private TextView top_tv1;
    private TextView qx_status_tv;
    private TextView qx_value_tv;
    private TextView qx_unit_tv;
    private TextView qx_mesureCount_tv;

    private TemHisChartView temhischarview;


    @Override
    protected void initView() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                top_tv1_Click();
            }
        });

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        qx_status_tv=findViewById(R.id.qx_status_tv);
        qx_value_tv=findViewById(R.id.qx_value_tv);
        qx_unit_tv=findViewById(R.id.qx_unit_tv);
        qx_mesureCount_tv=findViewById(R.id.qx_mesureCount_tv);


        temhischarview = findViewById(R.id.temhischarview);
        temhischarview.setItemSelectListener(new TemHisChartView.ItemSelectListener() {
            @Override
            public void selectChange(int selectIndex, TemHisChartView.ItemBean itemBean) {
                Log.i(TAG,"selectIndex = "+selectIndex+"---"+itemBean.toString());

                ViewUtils.setTextViewStr(qx_status_tv, TemUtils.getResultDesc(itemBean.value));
                if (TemUtils.getUerUnit()==0){
                    //C
                    ViewUtils.setTextViewStr(qx_value_tv, TemGunUtil.getDisTemC(itemBean.value)+"");
                    ViewUtils.setTextViewStr(qx_unit_tv, "°C");
                }else {
                    //F
                    ViewUtils.setTextViewStr(qx_value_tv, TemGunUtil.getDisTemF(itemBean.value)+"");
                    ViewUtils.setTextViewStr(qx_unit_tv, "°F");
                }
                ViewUtils.setTextViewStr(qx_mesureCount_tv, String.format(CommonLib.getString(R.string.gong_n_ciceliang),itemBean.itemTotalNum+""));
            }
        });

        temhischarview.setLeftRightScorllListener(new TemHisChartViewForm.LeftRightScorllListener() {
            @Override
            public void leftLoad() {
                LogUtils.i(TAG,"---temhischarview--左加载");
                getReportData();
            }

            @Override
            public void rightLoad() {
                LogUtils.i(TAG,"---temhischarview--右加载");
            }
        });

        temhischarview.setUnitModel(TemUtils.getUerUnit());

       // addCeshiValue();
    }

    private int quxianIndex1 = 1;//1额温曲线 2耳温曲线
    private int quxianIndex2 = 1;
    //顶部标题被点击
    private void top_tv1_Click() {
        quxianIndex2=quxianIndex1;
        WheelViewDialog wheelViewDialog = new WheelViewDialog(this);
        wheelViewDialog.setGravity(Gravity_BUTTOM);
        List<String> list = new ArrayList<>();
        list.add(CommonLib.getString(R.string.ewenquxian));
        list.add(CommonLib.getString(R.string.erwenquxian));
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                quxianIndex2 = selectedIndex;
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                Log.i(TAG, "cancel");
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                Log.i(TAG, "ok");
                dialogView.cancel();
                quxianIndex1 = quxianIndex2;
                if (temhischarview!=null){
                    temhischarview.setDatas(null,-1);
                }
                updateTopTitle();
                resetStartPageIndex();
                getReportData();
            }
        });
        wheelViewDialog.setSelectPosition(quxianIndex1-1);
        wheelViewDialog.show();
        wheelViewDialog.setBackgroundColor(Color.parseColor("#0176ff"));
    }



    //更新显示顶部标题栏 1额温曲线 2耳温曲线
    private void updateTopTitle(){
      ViewUtils.setTextViewStr(top_tv1,quxianIndex1==1?CommonLib.getString(R.string.ewenquxian):CommonLib.getString(R.string.erwenquxian));
    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_temperature_curve;
    }

    private void addCeshiValue() {
        List<TemHisChartView.ItemBean> datas = new ArrayList<>();
        Random random=new Random();
        for (int i=0;i<40;i++){
            TemHisChartView.ItemBean itemBean = new TemHisChartView.ItemBean();
            itemBean.index = i;
            itemBean.value = 34.0f + getRandom(random,0,6);
            itemBean.date = "i = " + i;
            itemBean.time = "17:30";
            itemBean.itemTotalNum = (int) getRandom(random,0,6);
            datas.add(itemBean);
        }
        temhischarview.setDatas(datas,0);
    }

    private float getRandom(Random random,float min,float max){
        return NumUtils.numBaoLiuWei0(random.nextFloat()*(max-min)+min);
    }




    //曲线跳转到详情页面
    public void quxian2right(View view) {
        TemHisChartView.ItemBean selectItem = temhischarview.getSelectItem();
        if (selectItem==null){
            return;
        }
        LogUtils.i(TAG,"--quxian2right--选中的item = "+selectItem.toString());

        if (selectItem.itemTotalNum == 1){
            //如果今天只有一条数据就直接跳转到 详情页面
            DetailShowActivity.openDetailShowActivity(this,selectItem.testDate,selectItem.tempType,selectItem.value);
        }else {
            getThisDayDatas(selectItem.testDate,selectItem.tempType);
        }
    }

    private static final long ONE = 24*3600*1000L;
    //获取今天的所有测量数据
    private void getThisDayDatas(String testDate, final int tempType) {

        long userId = TemUtils.getUerID();
        long timeL = TimeUtils.parseFormatter1Time(testDate).getTime();
        final String startTime = TimeUtils.formatTime3(timeL);
        String endTime = TimeUtils.formatTime3(timeL + ONE);
        LogUtils.i(TAG,"--getThisDayDatas--startTime = " + startTime + "--endTime = "+endTime+"---tempType = "+tempType);
        showLoadingDialog("加载中...");
        TGNetUtils.getInstance().getTempertureData12(this,userId,tempType,startTime,endTime, new HttpResponse<GetTempertureData>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, GetTempertureData getTempertureData, String s) {
                LogUtils.i(TAG,"---getTempertureData12-- s = " + s);
                LogUtils.i(TAG,"---getTempertureData12-- getTempertureData = "+getTempertureData);
                dissLoadingDialog();
                if (isRequestSuccess&&getTempertureData!=null&&getTempertureData.resultCode==0
                        &&getTempertureData.getResultData()!=null&&getTempertureData.getResultData().getData()!=null){
                    //成功
                    showListView(startTime,getTempertureData.getResultData().getData(),tempType);
                }
            }
        });
    }

    private ListViewDialog mListViewDialog;
    //展示列表
    private void showListView(String startTime, List<GetTempertureData.ResultDataBean.DataBean> data, final int tempType) {

        if (mListViewDialog==null){
            mListViewDialog=new ListViewDialog(this);
            mListViewDialog.setListener(new ListViewDialog.IListener() {
                @Override
                public void cancel(Dialog dialogView) {
                    dialogView.dismiss();
                }

                @Override
                public void selectItem(Dialog dialogView, GetTempertureData.ResultDataBean.DataBean dataBean) {
                    LogUtils.i(TAG,"---选中了 dataBean="+dataBean);
                   // dialogView.dismiss();
                    DetailShowActivity.openDetailShowActivity(TemperatureCurveActivity.this,dataBean.testDate,tempType,dataBean.getTemperature());
                }
            });
        }
        mListViewDialog.setTopTitle(startTime);
        mListViewDialog.setDatas(data);
        mListViewDialog.show();
        mListViewDialog.setTopTitle(startTime);
    }


    @Override
    protected void initData() {
        resetStartPageIndex();
        getReportData();
    }

    //重置时间
    private void resetStartPageIndex(){
        startPageIndex_ewen = 0;
        startPageIndex_erwen = 0;
    }


    private int startPageIndex_ewen;//额温
    private int startPageIndex_erwen;//耳温

    //获取数据
    private void getReportData() {
        //quxianIndex1 = 1;//1额温曲线 2耳温曲线

        final long userId = TemUtils.getUerID();    //用户id
        final int tempType = (quxianIndex1==1?2:1); //温度类型(1:耳温,2:额温)

        final int startPageIndex;

        if (tempType==1){
            //耳温
            startPageIndex = startPageIndex_erwen;
        }else {
            //额温
            startPageIndex = startPageIndex_ewen;
        }

        TGNetUtils.getInstance().getEveryDayLastDatas(this, userId, tempType,startPageIndex, new HttpResponse<GetTempertureData3>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, final GetTempertureData3 getTempertureData3, String s) {

                LogUtils.i(TAG,"---userId = "+userId+"--tempType = "+tempType+"--startPageIndex = "+startPageIndex);

                LogUtils.i(TAG,"---getReportData--s = "+s);

                if (isRequestSuccess&&getTempertureData3!=null&&getTempertureData3.resultCode==0){
                    LogUtils.i(TAG,"--getEveryDayLastDatas--成功--getTempertureData3="+getTempertureData3.toString());
                    if (getTempertureData3.resultData!=null&&getTempertureData3.resultData.data.size()>0){
                        dealDatas(tempType,getTempertureData3.resultData.data);
                    }
                }else {
                    LogUtils.i(TAG,"--getEveryDayLastDatas--失败");
                }
            }
        });
    }


    //处理网络请求回来的数据
    private void dealDatas(int tempType, List<GetTempertureData3.ResultDataBean.DataBean> resultData) {

        //01
        if (tempType==1){//(1:耳温,2:额温)
            //耳温
            startPageIndex_erwen++;
        }else {
            //额温
            startPageIndex_ewen++;
        }

        //02
        List<TemHisChartView.ItemBean> datas = new ArrayList<>();
        for (GetTempertureData3.ResultDataBean.DataBean bean:resultData){
            TemHisChartView.ItemBean itemBean = new TemHisChartView.ItemBean();
            itemBean.value = bean.temperature;


//            if (itemBean.value>41.7){
//                itemBean.value=41.7f;
//            }else if (itemBean.value<32.5){
//                itemBean.value=32.5f;
//            }

            long time=TimeUtils.parseFormatter1Time(bean.testDate).getTime();
            itemBean.testDate = bean.testDate;
            itemBean.date = TimeUtils.formatDate_yr(CommonLib.APPContext,time);
            itemBean.time = TimeUtils.formatTime_shf(CommonLib.APPContext,time);
            itemBean.itemTotalNum = bean.dayCount;
            itemBean.tempType = bean.tempType;
            datas.add(itemBean);
        }

        if (temhischarview!=null){
            temhischarview.addDatas(datas);
        }






    }
}
